package com.john.ishadowsocks.auto.data;

import org.apache.commons.lang3.StringUtils;

/**
 * 〈一句话功能简述〉<br/>
 * 〈功能详细描述〉
 *
 * @author jiangguangtao on 2016/5/20.

 */
public class ServerItem {
    //服务器地址
    private String server;
    //端口
    private Integer serverPort;
    //连接密码
    private String password;
    //加密方式
    private String encryptMethod;
    //备注
    private String remarks;
    private boolean auth = false;
    //状态 是否正常
    private String status;

    public ServerItem() {
    }

    public ServerItem(String server, Integer serverPort) {
        this.server = server;
        this.serverPort = serverPort;
    }

    public ServerItem(String server, Integer serverPort, String password, String encryptMethod) {
        this.server = server;
        this.serverPort = serverPort;
        this.password = password;
        this.encryptMethod = encryptMethod;
    }

    public String getServer() {
        return server;
    }

    public void setServer(String server) {
        this.server = server;
    }

    public Integer getServerPort() {
        return serverPort;
    }

    public void setServerPort(Integer serverPort) {
        this.serverPort = serverPort;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEncryptMethod() {
        return encryptMethod;
    }

    public void setEncryptMethod(String encryptMethod) {
        this.encryptMethod = encryptMethod;
    }

    public String getRemarks() {
        return remarks;
    }

    public void setRemarks(String remarks) {
        this.remarks = remarks;
    }

    public boolean isAuth() {
        return auth;
    }

    public void setAuth(boolean auth) {
        this.auth = auth;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    /**
     * 获取此代理的key值，能够唯一标识此代理的字符串
     * <br/>ip:端口 为key
     *
     * @return
     */
    public String getRowKey() {
        return server + ":" + serverPort;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        ServerItem that = (ServerItem) o;

        if (!server.equals(that.server)) return false;
        if (!serverPort.equals(that.serverPort)) return false;
        if (password != null ? !password.equals(that.password) : that.password != null) return false;
        return !(encryptMethod != null ? !encryptMethod.equals(that.encryptMethod) : that.encryptMethod != null);

    }

    @Override
    public int hashCode() {
        int result = server.hashCode();
        result = 31 * result + serverPort.hashCode();
        result = 31 * result + (password != null ? password.hashCode() : 0);
        result = 31 * result + (encryptMethod != null ? encryptMethod.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "ServerItem{" +
                "server='" + server + '\'' +
                ", serverPort=" + serverPort +
                ", password='" + password + '\'' +
                ", encryptMethod='" + encryptMethod + '\'' +
                ", remarks='" + remarks + '\'' +
                ", auth=" + auth +
                ", status=" + status +
                '}';
    }

    public boolean isValid() {
        if (StringUtils.isBlank(server)
                || null == serverPort
                || StringUtils.isBlank(password)
                || StringUtils.isBlank(encryptMethod)) {
            return false;
        }

        if (!StringUtils.equals("正常", status)) {
            return false;
        }
        return true;
    }
}
